// /////////////////////////////////////////////////////////////////////////////
//
// Name:            EventSlot.java
// Author:          Michael Bartsch
//
// Desc :           Defines the EventSlot class.
//
// License:         Copyright (C) 2009 Michael Bartsch
//
//                  This software is provided 'as-is', without any express or
//                  implied warranty. In no event will the authors be held
//                  liable for any damages arising from the use of this
//                  software.
//
//                  Permission is granted to anyone to use this software for
//                  any purpose, including commercial applications, and to
//                  alter it and redistribute it freely, subject to the
//                  following restrictions:
//
//                  1. The origin of this software must not be misrepresented;
//                  you must not claim that you wrote the original software. If
//                  you use this software in a product, an acknowledgment in the
//                  product documentation would be appreciated but is not
//                  required.
//
//                  2. Altered source versions must be plainly marked as such,
//                  and must not be misrepresented as being the original
//                  software.
//
//                  3. This notice may not be removed or altered from any source
//                  distribution.
//
// /////////////////////////////////////////////////////////////////////////////

// Package
package net.dotsimplicity.events;

// Import classes
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


// EventSlot class
class EventSlot
{
	// Public methods
	/**
	 * Constructs an EventSlot with the given object and method as the base.
	 */
	public EventSlot(Object object, Method method)
	{
	   this.object = object;
	   this.method = method;
	}
	
	/**
	 * Returns the object on which this EventSlot is based.
	 */
	public Object getObject()
	{
	   return this.object;
	}
	
	/**
	 * Returns the method on which this EventSlot is based.
	 */
	public Method getMethod()
	{
	   return this.method;
	}
	
	/**
	 * Emits an event signal with the given arguments.
	 * @param arguments      Arguments which will be transmitted together with the signal
	 */
	public void emitEventSignal(Object[] arguments)
	{
	   try
	   {
		  this.method.invoke(this.object, arguments);
	   }
	   
	   // An IllegalArgumentException indicates that we're using a Class object as instance. This 
	   // behavior is allowed, because it's faster than checking if we're dealing with a static 
	   // method for every slot. Calling static methods will receive a slight time penalty, but
	   // static methods are (probably) a minority.
	   catch(IllegalArgumentException e)
	   {
	      try
	      {
	         this.method.invoke(null, arguments);
	      }
	      catch(NullPointerException e2)
		  {
		     throw new NullPointerException("EventSlot(" + ((Class<?>)(this.object)).getName() + ", " +
		    		                        this.method.getName() + ").emitEventSignal: " + 
		    		                        "Non-static method called in a static way");
		  }
	      catch(IllegalAccessException e2)
		  {
			 System.out.println("EventSlot(" + this.object.getClass().getName() + 
		    		            ", " + this.method.getName() + ").emitEventSignal: Method inaccessible\n");
		  } 
		  catch (InvocationTargetException e2)
		  {
		     System.out.println("EventSlot(" + this.object.getClass().getName() + 
			                    ", " + this.method.getName() + ").emitEventSignal: Method completed abruptly\n");
		  }
	   }
	   
	   // Catch remaining exceptions.
	   catch(IllegalAccessException e)
	   {
		  System.out.println("EventSlot(" + this.object.getClass().getName() + 
				             ", " + this.method.getName() + ").emitEventSignal: Method inaccessible\n");
	   } 
	   catch (InvocationTargetException e)
	   {
		   System.out.println("EventSlot(" + this.object.getClass().getName() + 
		             ", " + this.method.getName() + ").emitEventSignal: Method completed abruptly\n");
	   }
	}
	
    // Private members
	private Object object;
	private Method method;
}

// End of File
